Weighted Fit to a Polynomial

June 9, 2021

In this script we will fit a polynomial to a set of experimental data. The fit will be weighted by the measurement uncertainties. This tutorial is very similar to the weighted linear fit tutorial, so let's get to it!

Updated by Jordan Andrews on June 9, 2021 with the use of np.polynomial.Polynomial([a, b, c]).

Enter the data. Suppose that $y$ is the number of counts in some kind of counting experiment. In that case, the error in $y$ is just the square root of the number of counts ($\sqrt{y}$).

Plot the data using plt.errorbar(x,y,e).

To do the actual fit, we will use the 'curve_fit()' function from, the SciPy module. This way of fitting is very nice because we use it for all types of fit models (linear, polynomial, line-in-parameter fits, and nonlinear fit). It is capable of doing both unweighted and weighted fits and it will return uncertainties in the fit parameters.

The first step is to define a function for the model that we will fit our data to. In this case, the model is a quadratic.

Here is the actual command to execute the fit. At a minimum, curve_fit() requires as inputs the function that defines the model, the $x$-data, and the $y$-data. The statement below tells curve_fit() to return a list of the the best-fit parameters and the covariance matrix which will be used to determine the error in the fit parameters.

Print the best-fit parameters.

The uncertainties of the best-fit parameters are determined from the square roots of the diagonal elements of the covariance matrix. We can select the diagonal elements using (Note the use of the unicode character \u0394 to print $\Delta$):

NumPy has a nice package for polynomials, called polynomial. There are six different polynomial types in this package. For our case, we are dealing with a simple power series. You can use the Polynomial constructor for this. y = np.polynomial.Polynomial([a, b, c]) results in $y = a + b\,x + c\,x^2$.

Here's the best-fit fucntion obtained using a_fit from curve_fit() and the built in polynomial package of NumPy.

This gives us an easy way to plot the fit on top of the data.

All of this has produced an "unweighted" fit to the data. To include weights, all we need to do is include another option in curve_fit(). Everything else is exactly the same! The new option is sigma and it is simply a list of the errors in the $y$-values. Note that many fitting routines require you to provide the actual weights as $1/\sigma^2$. That is not the case here. You just have to provide the absolute $y$-uncertainties.

Here's a block of code that does the fit, extracts the best fit function, the best-fit parameters, the uncertainties, and then plots the result.